<template>
    <div class="app-container home">
        <van-cell-group>
            <van-field v-model="oldPwd" type="password" label="原密码" placeholder="请输入原密码" />
            <van-field v-model="password" type="password" label="新密码" placeholder="请输入新密码" />
            <van-field v-model="newPassword" type="password" label="确认新密码" placeholder="确认新密码" />
        </van-cell-group>
        <div class="btn">
            <van-button color="#66d6ff" @click="EditPwd" type="primary" block>确 定</van-button>
        </div>
    </div>
</template>

<script setup name="reset">
import { editPwd } from '../../api/agent'
import { showSuccessToast, showToast } from 'vant'
import JSEncrypt from 'jsencrypt'
const oldPwd = ref('')
const value = ref('')
const userInfo = ref({})
const password = ref('')
import store from '@/store'
import router from '../../router'

const newPassword = ref('')
// const key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHsl/hiXgs9TbfNgV502i8o9xqhXNpnP0n+TdkKzVbDhlu9GyiuRD+Qsy4917ilq8f8KmJBO3DpkrxypXGttDH8Re5lYYcD88GMuWRZpdTCkcNbjalwO/k9gyKjJo/z4KthvfwtGbJhs3QsX5jYNQXyvf86EOcf9oA+iPOKi2NxwIDAQAB'

const EditPwd = () => {
    if (!oldPwd.value) {
        showToast('请输入旧密码')
        return
    }
    if (!password.value) {
        showToast('请输入新密码')
        return
    }
    if (password.value !== newPassword.value) {
        showToast('两次新密码不一致,请检查')
        return
    }
    const key =
        'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAicjwP+F30h1RmMZuAjKIW7+pJxK6NlhvsNks28X4/91iF7lBoZd57ZuN+310vaFxol8khFd5SDbnx7piqPxuAaUC/IygD4iPn2ZSSA5pqqdQYodJVckyh4PNjbh6lDkjYNoAGhz1BY+AiQz2uPTNFnOqmDu1ZfhoQCWMhSleW0zikybl3KRcczDja96+DmKkssXAYN5clnnZMcBjuWOwBz5pgHiQEQTatdWm/un3SR366LrnKAXYUyAEUxwuEA9qtiaDvKgLzSl84FqmvU6cLzmLJM+b+++0CONoT0IR9I6XFkF/wErnd8Y+gEB4oQEURe39BUjl5VcxSJxf1HorSQIDAQAB'
    // 调注册接口
    let jse = new JSEncrypt()
    jse.setPublicKey(key)
    let newPassword1 = jse.encrypt(password.value)
    let oldPassword = jse.encrypt(oldPwd.value)
    console.log(store.state.user.userInfo)
    editPwd({
        username: store.state.user.userInfo.agentBean.phoneNo,
        oldPassword: oldPassword,
        newPassword: newPassword1
    }).then(res => {
        showSuccessToast('修改成功，请重新登录')
        uni.reLaunch({
            url: '/pages/login'
        })
    })
}
</script>

<style scoped lang="scss">
.app-container {
    padding: 10px;
    .btn {
        margin-top: 20px;
    }
}
</style>
